<!DOCTYPE html>
<html>
<head>
  <title></title>
  <meta charset="utf-8">
  <link rel="stylesheet" href="../css/login.css">


</head>
<body style="background:url(../image/bg.jpg) no-repeat;background-size:110% 100%;">
<div class="login_box">
 <h2>登录</h2>
        <div class="form-control"><input type="text"   id="email"   placeholder="邮箱"></div>
         <div class="form-control"><input type="password"  id="password"   placeholder="密码"></div>
         <button type="button" name="button" onclick="login()">登录</button>
         <a href="register.html">还没有账号？立即注册>>></a>

</div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="../script/main.js"></script>
<script>
   apiready=function(){

}
 function login(){
    var email=$("#email").val();
    var password=$("#password").val();
    var deviceId=api.deviceId;
    console.log(deviceId);
    

    if (email!=""&&password!="") {
       var url='/api/paAdmin/login?email='+email+'&pwd='+password+'&deviceId='+deviceId;
       var functionRet=function(ret){
         toast("登录成功！","bottom");
         var jwt=ret.jwt;
            $api.setStorage('deviceId',deviceId);
            $api.setStorage('jwt', ret.jwt);
            $api.setStorage('aid',ret.aid);
            $api.setStorage('flag',ret.flag);
            $api.setStorage('nickname',ret.nickname);
            $api.setStorage('userHeaderImageUrl',ret.userHeaderImageUrl);
              api.openFrame({
                  name: 'main',
                  url: '../html/main.html',
                  rect: {
                      duration:1000,
                      x: 0,
                      y: 0,
                      w: 'auto',
                      h: 'auto'
                  },
                  pageParam: {
                      name: 'test'
                  },
                  bounces: true,
                  bgColor: 'rgba(0,0,0,0)',
                  vScrollBarEnabled: true,
                  hScrollBarEnabled: true
              });


       }
       var functionErr=function(err){
         console.log(err.statusCode);
         if(err.statusCode==411){
           toast("已在其他设备登录","bottom");
         }else if(err.statusCode==410){
           toast("账号密码错误，请重新输入","bottom");
         }else if(err.statusCode==404){
           toast("账号不存在","bottom");
         }else{
           toast("登录失败","bottom");
         }
       }

       getlogin(url,functionRet,functionErr);
    }
    else {
    toast("请填写相关信息！","bottom")
    }
 }


    function getlogin(url,functionRet,functionErr){
      var jwt=$api.getStorage('jwt');
      console.log(jwt);
      api.ajax({
          url: 'http://113.17.111.241:9999/universityfacade'+url,
          method: 'GET',
          headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": "Bearer"+" "+jwt
          }
      },function(ret, err){
          if (ret) {
              functionRet(ret);
          } else {
            functionErr(err);
          }
      });

     }


</script>

</html>
